package com.hulk.other.Thread.blockingqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 阻塞队列测试
 * @author Administrator
 *
 */
public class BlockingQueueTest {

	public static void main(String[] args) throws InterruptedException {
		// 声明一个容量为10的缓存队列
		BlockingQueue queue = new LinkedBlockingQueue(10);
		Producer producer1 = new Producer(queue);
//		Producer producer2 = new Producer(queue);
//		Producer producer3 = new Producer(queue);
//		Producer producer4 = new Producer(queue);
//		Producer producer5 = new Producer(queue);
//		Producer producer6 = new Producer(queue);
		Consumer consumer1 = new Consumer(queue);
//		Consumer consumer2 = new Consumer(queue);
//		Consumer consumer3 = new Consumer(queue);
		// 借助Executors
		ExecutorService service = Executors.newCachedThreadPool();
//		ExecutorService  pool =  Executors.newFixedThreadPool(10);
		// 启动线程
		service.execute(producer1);
//		service.execute(producer2);
//		service.execute(producer3);
//		service.execute(producer4);
//		service.execute(producer5);
//		service.execute(producer6);
		service.execute(consumer1);
//		service.execute(consumer2);
//		service.execute(consumer3);

		// 执行10s
//		Thread.sleep(1 * 1000);

//System.out.println(queue.size());
//		producer1.stop();
//		producer2.stop();
//		producer3.stop();

		Thread.sleep(20000);
		// 退出Executor
		service.shutdown();
	}

}
